#include "WallTurret.h"

#define PI 3.14f
#define GUN_ANGLE PI/6
#define R_HOUSE 30
#define TIME_FIND 500
#include "Box.h"
#include "Sprite.h"
#include "ShareComponents.h"
#include "BulletManager.h"
#include "Camera.h"

WallTurret::WallTurret(Box* _box) : Object(_box,ObjEnemy_STATIC)
{

}


WallTurret::~WallTurret(void)
{
}


void WallTurret::init()
{
	_sAngle = RSManager::getInstance()->getSprite(BLOCK_HOUSE_ANGLE);
	_sSimple = RSManager::getInstance()->getSprite(BLOCK_HOUSE_SIMPLE);
	_sStart = RSManager::getInstance()->getSprite(BLOCK_HOUSE_START);		
	_sprite =NULL;
	delay = 0;
	gundelay = 0;
	_hits=8;
	_isAlive=true;
	_obj= ObjEnemy_STATIC;

}
void WallTurret::update()
{
	//set state
	if (gl_camera->getLeftSide()>_box->_position.x -10||gl_camera->getRightSide()<_box->_position.x+_box->_size.x+40){
		state= SIMPLE;
	}
	else{
		if(state ==SIMPLE) state= START;
		delay += GAME_TIME;
		if (state == START && delay> 100){
			delay=0;
			state = GUN;
			_sprite = _sAngle;
			_sprite->Reset(7);
		}
	}
	// process state
	switch (state)
	{
		case SIMPLE:
			if(_sprite==NULL || _sprite!=_sSimple) _sprite = _sSimple;
				delay += GAME_TIME;
				if(delay>200)
				{
					delay=0;
					_sprite->Next();
				}break;
		case START: _sprite=_sStart;break;
		case GUN:
			delay += GAME_TIME;
			if(delay>300)
			{
				delay=0;
				action();
			}break;
		default:
			break;
	}
	//set die
	if(_hits <= 0) _isAlive = false;
}

void WallTurret::action()
{
	int last_index = GetNumFrame(SetAngle(gl_player));//next index
	int index = _sprite->GetCurrentIndex(); //current index
	//check cases
	if (last_index > index)
	{
		if (last_index <= 15 && last_index>=12 && index>=0 &&index<=4)index--;
		else index++;
	}
	else if (last_index < index)
	{
		if (last_index >=0 && last_index <=3 && index <= 15 && index>=12) index++;
		else index--;
	}
	else index = last_index;	
	// some except cases
	if (index<0) index = 15;
	if (index>15) index =0;
	//reset sprite
	_sprite->Reset(index);
	//carry gun and shoot bomb! bomb! bomb! but wait 800 ms
	gundelay += GAME_TIME;
	if (index==last_index && gundelay > 200)
	{
		index = ConvertIndex(index);
		gundelay = 0;
		D3DXVECTOR2 gun_direction(cosf(index*GUN_ANGLE) * R_HOUSE, sinf(index*GUN_ANGLE) * R_HOUSE);
		BulletManager::getInstance()->shoot(B_E,_obj,_box->getCenterPos() + gun_direction,index*GUN_ANGLE);
	}
		
}
//get suitable number of frame for every position of player
int WallTurret::GetNumFrame(float angle)
{
	//angle = pi, pi/2, 2pi, 3pi/2...
	return (int)(angle * 8 / PI) % 16;
}
//set Angle from the position of player
float WallTurret::SetAngle(Box* boxContra)
{
	float doi = boxContra->getCenterPos().y - _box->getCenterPos().y;
	float ke = boxContra->getCenterPos().x - _box->getCenterPos().x;

	return atan2(doi, ke)>0? atan2(doi, ke):2*PI +atan2(doi,ke);
}
void WallTurret::render()
{
	if(_isAlive)
	_sprite->Render(_box->_position);	
}

Box* WallTurret::getPos()
{
	return _box;
}
// convert
int WallTurret::ConvertIndex(int index)
{
	switch (index)
	{
	case 3:
	case 4: return 3;
	case 5: return 4;
	case 6: return 5;
	case 7:
	case 8:return 6;
	case 9: return 7;
	case 10:return 8;
	case 11:
	case 12:return 9;
	case 13: return 10;
	case 14: return 11;
	case 15: return 0;
	default:
		break;
	}
}

Box* WallTurret::getCollisionBox()
{
	_colBox->_position = _box->_position + D3DXVECTOR2(15,15);
	_colBox->_size = _box->_size/2;
	_colBox->_v = _box->_v;
	return _colBox;
}
